"""
@version:python3
@author:大橘为重
@file:47-QFormLayout.py
@time:2022/4/14 21:11
"""
# 0、导入需要的包和模块
from PyQt5.Qt import *
import sys

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("")
        self.resize(500, 500)
        self.setup_ui()
    def setup_ui(self): # 添加子控件以及子控件的配置操作
        name_label = QLabel("姓名(&n)")
        # age_label = QLabel("年龄(&g)")
        name_le = QLineEdit()
        age_sb = QSpinBox()
        submit_btn = QPushButton("提交")
        sex_label = QLabel("性别")
        male_rb = QRadioButton("男")
        female_rb = QRadioButton("女")
        h_layout = QHBoxLayout()
        # h_layout.addWidget(sex_label)
        h_layout.addWidget(male_rb)
        h_layout.addWidget(female_rb)

        name_label.setBuddy(name_le) # 这里用setBuddy关联两个控件
        # age_label.setBuddy(age_sb)

        layout = QFormLayout()

        self.setLayout(layout)

        # layout.addWidget(name_label)
        # layout.addWidget(name_le)
        # layout.addWidget(age_label)
        # layout.addWidget(age_sb)

        layout.addRow(name_label,name_le)
        # layout.addRow(age_label,age_sb)
        layout.addRow("年龄(&g)",age_sb) # 自动添加标签并关联
        layout.addRow(submit_btn)
        layout.addRow(sex_label,h_layout)

        # 插入行的操作
        # layout.insertRow()

        print(layout.rowCount())
        print(layout.getWidgetPosition(age_sb)) # 返回值前一个代表行数，后面一个代表角色值
        print(layout.getLayoutPosition(h_layout)) # 返回值前一个代表行数，后面一个代表角色值

        layout.setFormAlignment(Qt.AlignLeft | Qt.AlignBottom)

# 1、创建一个应用程序对象
app = QApplication(sys.argv)

# 2、控件操作
# 创建控件、设置控件、事件和信号的处理
# 2.1 创建控件
window = Window()
# 2.2 设置控件


# 2.3 展示控件
window.show()

# 3、应用程序的执行，进入到消息循环
sys.exit(app.exec_())